<%@ page import="custom.*, java.util.*, java.sql.*, javax.sql.*, javax.naming.*"%>
<html>
<%	String str_sid = request.getParameter("specialization_id");
	String str_mid = request.getParameter("major_id");
	// if we are from the analytics - specialization page
	int id = 0;
	if(str_sid != null) {
		id = Integer.parseInt(str_sid);
	} else if(str_mid != null) // if we are from the Discipline Analytics page
	{
		id = Integer.parseInt(str_mid);
	}
	// connect to the database
	InitialContext cxt = new InitialContext();
	DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/postgres");
	Connection conn = ds.getConnection();
	PreparedStatement stmt = null;
%>
<head>
<title>Application Review</title>
</head>
<body>
<script type="text/javascript">


function insertAfter( referenceNode, newNode )
{
    referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
} 

function insertRow(parent, leftStr, rightStr)
{
	var trNode = document.createElement("tr");
	parent.appendChild(trNode);
	var tdNodeLeft = document.createElement("td");
	tdNodeLeft.innerHTML = leftStr;
	trNode.appendChild(tdNodeLeft);
	var tdNodeRight = document.createElement("td");
	tdNodeRight.innerHTML = rightStr;
	trNode.appendChild(tdNodeRight);
}

// on-click function to show details
// use AJAX to send and process request
// data received as JSONObject
function getAppDetails(id)
{
	var show;
	var buttonId = "butt_" + id;
	var button = document.getElementById(buttonId);
	var buttonValue = button.getAttribute("value");
	if(buttonValue == "Show application")
		show = true;
	else 
		show = false;
	// if we are showing details
	if(show) {
		// set the button text
		button.setAttribute("value", "Hide application");
		
		// use AJAX to request applicant details
		var xmlHttp = new XMLHttpRequest();
		var url = "getApplicationDetails.jsp";
		url = url + "?applicant_id=" + id;
		xmlHttp.onreadystatechange = function(id) {
			return function() {
				if(this.readyState == 4)
				{
					var responseDoc = xmlHttp.responseText;
					var r = eval('('+ responseDoc +')');
					
					// node inserted after the <tr> of applicant's name and show button
					var node = document.createElement("tr");
					node.setAttribute("id", "tr_" + id + "_d");
					// td for the extra tr
					var nodetd = document.createElement("td");
					nodetd.setAttribute("colspan", "2");
					node.appendChild(nodetd);
					// create table inside the td
					var nodeTable = document.createElement("table");
					nodeTable.setAttribute("border", "1");
					nodetd.appendChild(nodeTable);
					var tbody = document.createElement("tbody");
					nodeTable.appendChild(tbody);
					// first name
					insertRow(tbody, "First name:", r.first_name);
					// middle name
					insertRow(tbody, "Middle name:", r.middle_name);
					// last name
					insertRow(tbody, "Last name:", r.last_name);
					// country of citizenship
					insertRow(tbody, "Country of citizenship:", r.cc_country);
					// country of residence
					insertRow(tbody, "Country of residence:", r.cr_country);
					// street 
					insertRow(tbody, "Street:", r.street);
				
					// city 
					insertRow(tbody, "City:", r.city);
	
					// state 
					insertRow(tbody, "State:", r.state);
					// zip 
					insertRow(tbody, "Zip:", r.zip);
					// Area code 
					insertRow(tbody, "Area code:", r.area_code);
					// phone 
					insertRow(tbody, "Phone:", r.phone);
					// Country phone code 
					insertRow(tbody, "Country phone code:", r.country_phone_code);
					
					// blank row 
					insertRow(tbody, "&nbsp;", "&nbsp;");
					
					// degrees: 
					trNode = document.createElement("tr");
					tbody.appendChild(trNode);
					tdNodeLeft = document.createElement("td");
					tdNodeLeft.innerHTML = "Degree(s):";
					trNode.appendChild(tdNodeLeft);
					tdNodeRight = document.createElement("td");
					tdNodeRight.innerHTML = "&nbsp;";
					trNode.appendChild(tdNodeRight);

					for(var i=0; i < r.degrees.length; ++i)
					{
						// blank row 
						insertRow(tbody, "&nbsp;", "&nbsp;");
						// uni
						insertRow(tbody, "University:", r.degrees[i].university);
			
						// major
						insertRow(tbody, "Discipline:", r.degrees[i].major);

						// graduation_date
						insertRow(tbody, "Graduation date:", r.degrees[i].graduation_date);

						// gpa
						insertRow(tbody, "GPA:", r.degrees[i].gpa);

						// degree_type
						insertRow(tbody, "Degree type:", r.degrees[i].degree_type);

					}
					insertRow(tbody, "&nbsp;", "&nbsp;");
					//specialization:
					insertRow(tbody, "Specialization:", r.specialization);
					
					var afterNodeId = "tr_" + id;
					var afterNode = document.getElementById(afterNodeId);
					insertAfter(afterNode, node);
				}
			}
		} (id);
		
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		
	}
	else // hide application
	{
		button.setAttribute("value", "Show application");
		var detailsElementId = "tr_" + id + "_d";
		var detailsElement = document.getElementById(detailsElementId);
		detailsElement.parentNode.removeChild(detailsElement);
	}
}
</script>
<h2> Application Review </h2> <p> </p>
<%	// if we are from the analytics - specialization page
	if(str_sid != null) {
		stmt = conn.prepareStatement("SELECT specialization FROM specializations WHERE id = ?");
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		if(rs.next() ) {
%>
			<h4>Specialization: <%=rs.getString(1) %></h4> <p> </p>		
<% 			
			rs.close();
		}
	} else if(str_mid != null) // if we are from the Discipline Analytics page
	{
		stmt = conn.prepareStatement("SELECT major FROM majors WHERE id = ?");
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		if(rs.next() ) {
%>
			<h4>Discipline: <%=rs.getString(1) %></h4> <p>	</p>
<% 		
			rs.close();
		}
	}
%>
<h4>Applicant(s):</h4> <p> </p>
<table > <tbody>
<% 
	// if we are from the analytics - specialization page
	if(str_sid != null) {
		stmt = conn.prepareStatement("SELECT first_name, middle_name, last_name, id AS applicant_id FROM applicants WHERE specialization_id = ? ORDER BY last_name");
		stmt.setInt(1, id);
	} else if(str_mid != null) { // if we are from the Discipline Analytics page
		stmt = conn.prepareStatement("SELECT DISTINCT first_name, middle_name, last_name, D.applicant_id AS applicant_id FROM applicants AS A, degrees AS D WHERE A.id = D.applicant_id  AND major_id = ? ORDER BY last_name");
		stmt.setInt(1, id);
	} else // if we are not from any page - display all applicants
		stmt = conn.prepareStatement("SELECT DISTINCT first_name, middle_name, last_name, id AS applicant_id FROM applicants ORDER BY last_name");
	
	ResultSet rs = stmt.executeQuery();
	while(rs.next() ) {
		int applicant_id = rs.getInt("applicant_id");	
%>	
	<tr id="<%="tr_" + applicant_id %>" >
		<td  valign="top"><%=rs.getString("first_name") + " " + rs.getString("middle_name") + " " + rs.getString("last_name") %> </td>
		<td  width="*"> <form action=""> <input width="50" type="button" onclick="getAppDetails(<%=rs.getInt("applicant_id")%>);" id="<%="butt_" + rs.getInt("applicant_id") %>" value="Show application"> </form> </td>  </tr>
<% 	} %>
</tbody>
</table>
</body>
<% rs.close();
	stmt.close();
	conn.close();
%>
</html>